// components/com-date-picker.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //开始期间
    startAt: {
      type: String,
      value: "",
      observer: "onStartAt"
    },
    //结束时间
    endAt: {
      type: String,
      value: "",
      observer: "onEndAt"
    },
    isShow: {
      type: Boolean,
      value: false,
      observer: "onDateShow"
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    years: [],
    months: [],
    days: [],
    startValue: [],
    endValue: [],
    showPicker: false,
    tapIndex: "startTime",
    start: '',
    end: ''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onStartAt() {
      // this.data.startValue = this.getDateArrWithtime(this.data.startAt);
      // days = this.getTotalDayByMonth(this.data.years[this.data.startValue[0]], this.data.months[this.data.startValue[1]]);
    },
    onEndAt() {
      // this.data.endValue = this.getDateArrWithtime(this.data.endAt);
      // console.log(this.data.endValue);
    },
    onDateShow() {
      this.setData({
        showPicker: this.data.isShow
      });
    },
    //获取月的总天数--数组
    getTotalDayByMonth: function (year, month) {
      month = parseInt(month, 10);
      var d = new Date(year, month, 0);
      let totalDay = d.getDate();
      let daySource = [];
      for (let i = 1; i <= totalDay; i++) {
        daySource.push(String(i))
      }
      return daySource;
    },
    //根据时间2019-01-02  得到 ['2019','1','2']
    getDateArrWithtime: function (str) {
      let years = this.data.years, months = this.data.months, days = this.data.days;
      let arr = [];
      arr = (str).split('-');
      arr[0] = years.indexOf(parseInt(arr[0], 10));
      arr[1] = months.indexOf(parseInt(arr[1], 10));
      arr[2] = days.indexOf(parseInt(arr[2], 10));
      return arr;
    },
    // 监听变化
    bindPickerChange(e) {
      if (this.data.tapIndex == 'startTime') {
        this.data.startValue = e.detail.value;
      } else {
        this.data.endValue = e.detail.value;
      }
      this.calculate();
    },
    // 计算日期字符串
    calculate() {
      if (this.data.tapIndex == "startTime") {
        console.log(this.data.startValue)
        let curYear = this.data.years[this.data.startValue[0]];
        let curMonth = this.data.months[this.data.startValue[1]];
        let curDay = this.data.days[this.data.startValue[2]];
        this.data.startAt = curYear + '-' + (curMonth < 10 ? '0' + curMonth : curMonth) + '-' + (curDay < 10 ? '0' + curDay : curDay);

        this.setData({
          start: this.data.startAt
        })
      } else {
        let endCurYear = this.data.years[this.data.endValue[0]];
        let endCurMonth = this.data.months[this.data.endValue[1]];
        let endCurDay = this.data.days[this.data.endValue[2]];
        this.data.endAt = endCurYear + '-' + (endCurMonth < 10 ? '0' + endCurMonth : endCurMonth) + '-' + (endCurDay < 10 ? '0' + endCurDay : endCurDay);

        this.setData({
          end: this.data.endAt
        })
      }
    },
    // 切换
    onTapTimeIndex(e) {
      let startValue = this.data.startValue, endValue = this.data.endValue;
      this.data.tapIndex = e.currentTarget.dataset.type;
      if (this.data.tapIndex == "startTime") {
        startValue = this.getDateArrWithtime(this.data.startAt);
      } else {
        endValue = this.getDateArrWithtime(this.data.endAt);
      }
      this.setData({
        tapIndex: this.data.tapIndex,
        startValue: startValue,
        endValue: endValue,
      });
      // console.log("endValue => ", endValue);
    },
    // 点击完成按钮
    onSureClick(e) {
      if (new Date(this.data.startAt).getTime() <= new Date(this.data.endAt).getTime()) {
        this.triggerEvent('sureclick', {
          startAt: this.data.startAt,
          endAt: this.data.endAt,
        });
      } else {
        wx.showToast({
          title: '开始时间不能晚于结束时间',
          duration: 1500
        });
      }
    },
    // 点击蒙版
    onTouchmask: function (event) {
      this.triggerEvent('onTouchmask', {
        start: this.data.start,
        end: this.data.end
      });
    },
    // 拆分日期字符串
    splitDateStr: function (str) {
      const arr = str.split("-");
      return {
        year: parseInt(arr[0], 10),
        month: parseInt(arr[1], 10),
        day: parseInt(arr[2], 10)
      }
    },
    // 初始化数据
    init: function () {
      let start = this.splitDateStr(this.data.startAt);
      let end = this.splitDateStr(this.data.endAt);
      let years = [], months = [], days = [];
      for (let year = start.year; year <= end.year; ++year) {
        years.push(year);
      }
      for (let month = start.month; month <= end.month; ++month) {
        months.push(month);
      }
      for (let day = start.day; day <= end.day; ++day) {
        days.push(day);
      }

      this.setData({
        years: years,
        months: months,
        days: days,
        start: this.data.start || this.data.startAt,
        end: this.data.end || this.data.endAt,
        tapIndex: this.data.tapIndex,
      })
    }
  },
  /**
   * 组件生命周期
   */
  lifetimes: {
    // 在组件实例进入页面节点树时执行
    attached: function () {
      this.init();
    }
  }
})